﻿Public Class Servo
    Private Smin As Integer
    Private Smax As Integer
    Private Sval As Integer
    Private SSpeed As Integer

    Private Result As String
    Private Builder As New DataBuilder

    Private m_Device As Device = Nothing
    Private m_Channel As Integer = 0
    Public Property Channel() As Integer
        Get
            Return m_Channel
        End Get
        Set(ByVal value As Integer)
            m_Channel = value
        End Set
    End Property
    Public Property Host() As Device
        Get
            Return m_Device
        End Get
        Set(ByVal value As Device)
            m_Device = value
        End Set
    End Property
    Public Property Speed() As Integer
        Get
            Return SSpeed
        End Get
        Set(ByVal value As Integer)
            SSpeed = value
        End Set
    End Property
    Public Property Min() As Integer
        Get
            Return Smin
        End Get
        Set(ByVal value As Integer)
            Smin = value
            'Tx(10, Smin)
        End Set
    End Property
    Public Property Max() As Integer
        Get
            Return Smax
        End Get
        Set(ByVal value As Integer)
            Smax = value
            'Tx(20, Smax)
        End Set
    End Property
    Public Property Value() As Integer
        Get
            Return Sval
        End Get
        Set(ByVal value As Integer)
            Sval = value
            'Tx(0, value)
        End Set
    End Property
    Private Function Tx(ByVal Com As Integer, ByVal Value As Integer) As Boolean
        Try
            If m_Device IsNot Nothing And MyBase.DesignMode = False Then
                If m_Channel > 0 Then
                    Result = m_Device.ReadText(Builder.CreateThreeByte(Com + m_Channel, Value))
                    If Result = "OK" Then
                        Return True
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox("TX Error")
            Return False
        End Try
    End Function
    Private Function Rx() As Boolean
        Try
            If m_Device IsNot Nothing And MyBase.DesignMode = False Then
                If m_Channel > 0 Then
                    Result = m_Device.ReadText(Builder.CreateThreeByte(90 + m_Channel, 0))
                    Debug.Print(Result)
                    If Result > "" Then
                        Dim st() As String = Result.Split(",")
                        Smin = Val(st(0))
                        Smax = Val(st(1))
                        Sval = Val(st(2))
                        SSpeed = Val(st(3))
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Servo - RX")
            Return False
        End Try
    End Function
    Public Sub ReadAll()
        Rx()
    End Sub
    Public Sub WriteAll()
        Tx(0, Sval)
        Tx(10, Smin)
        Tx(20, Smax)
        Tx(30, SSpeed)
    End Sub
End Class
